package com.jz.community.basecomm.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.WeChatInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/5/8 16:50
 */
public class GetWeChatOpenIdTask extends RxTask<String, Integer, WeChatInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetWeChatOpenIdTask(Activity activity,
                               ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected WeChatInfo doInBackground(String... params) {
        WeChatInfo info = null;

        // 执行请求
        String result = OkHttpUtil.get(Constant.WEXIN_OPENID_URL, getParam(params[0]));

        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, WeChatInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(WeChatInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        ProgressDialogManager.showDialog(activity);
        super.onCancelled();
    }

    private HashMap<String, Object> getParam(String code) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("code", code);
        params.put("logintype", 1);
        return params;
    }
}
